package com.zhiguang.cms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiguang.cms.dao.ArticleMapper;
import com.zhiguang.cms.entity.Article;
import com.zhiguang.cms.entity.Category;
import com.zhiguang.cms.entity.Channel;
import com.zhiguang.cms.entity.Comment;
import com.zhiguang.cms.service.ArticleService;
import com.zhiguang.cms.utils.CmsContant;

@Service
public class ArticleServiceImpl  implements ArticleService{
	
	@Autowired
	ArticleMapper articleMapper;

	@Override
	public PageInfo list(int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,CmsContant.PAGE_SIZE);
		return new PageInfo<Article>(articleMapper.list());
	}

	@Override
	public Article getById(int id) {
		// TODO Auto-generated method stub
		return articleMapper.getById(id);
	}

	@Override
	public int check(int id, int status) {
		// TODO Auto-generated method stub
		return articleMapper.updateStatus(id,status);
	}

	@Override
	public int hot(int id, int status) {
		// TODO Auto-generated method stub
		return articleMapper.updateHot(id,status);
	}

	@Override
	public List<Channel> listChannels() {
		// TODO Auto-generated method stub
		return articleMapper.listChannels();
	}

	@Override
	public List<Category> listCat(int channelId) {
		// TODO Auto-generated method stub
		return articleMapper.listCats(channelId);
	}

	@Override
	public int add(Article article) {
		// TODO Auto-generated method stub
		return articleMapper.add(article);
	}

	@Override
	public PageInfo<Article> listByUser(Integer userId, int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,CmsContant.PAGE_SIZE);
		return new PageInfo<Article>(articleMapper.listByUser(userId));
	}

	@Override
	public void detete(int id) {
		// TODO Auto-generated method stub
		articleMapper.delete(id);
		
	}

	/**
	 * 
	 */
	@Override
	public List<Article> lastarticles(int n) {
		// TODO Auto-generated method stub
		return articleMapper.last(n);
	}

	@Override
	public PageInfo<Article> hotList(int page) {
		// TODO Auto-generated method stub

		PageHelper.startPage(page,CmsContant.PAGE_SIZE);
		return new PageInfo<Article>(articleMapper.hotList());
	}

	@Override
	public PageInfo<Article> listBy2C(int channelId, int categoryId, int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,CmsContant.PAGE_SIZE);
		return new PageInfo<Article>(articleMapper.listBy2C(channelId, categoryId));
	}

	@Override
	public int addComment(int articleId, String content, Integer userId) {
		// TODO Auto-generated method stub
		Comment comment = new Comment();
		comment.setArticleId(articleId);
		comment.setContent(content);
		comment.setUserId(userId);
		int addCnt= articleMapper.addComment(comment);
		// 发布评论成功
		if(addCnt>0)
			articleMapper.increaseCommentCnt(articleId);// 评论数量自增1
		return addCnt;
	}

	@Override
	public PageInfo<Comment> listComment(int articleId, int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, CmsContant.PAGE_SIZE);
		return new PageInfo<Comment>(articleMapper.listCommentByArticleId(articleId));
	}

}
